package com.example.helloworld;

import java.io.File;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class OpenPDF extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_open_pdf);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.open_pd, menu);
		return true;
	}
	
	public void openPDF(View view) {
		EditText edit =	(EditText)findViewById(R.id.openPDF);
		
		String fileName =	edit.getText().toString();
		
		if (fileName.length() == 0) {
			Toast.makeText(this, "Type PDF file name", Toast.LENGTH_SHORT).show();
			
			return;
		}
		
		File file =	new File(fileName);
		
		if (file.exists()) {
			Uri uri =	Uri.fromFile(file);
			Intent intent =	new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(uri, "application/pdf");
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			try {
				startActivity(intent);
			} catch (ActivityNotFoundException e) {
				Toast.makeText(this, "View not found", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_SHORT).show();
		}
	}

}
